﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;

namespace System.Web.Mvc
{
    /// <summary>
    /// 实现多按钮提交 表单: name="ColName:1700,15,cc:"  Action: MultiButton(Name="ColName" Argunent="id,cid,type")
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class MultiButtonAttribute : ActionMethodSelectorAttribute
    {
        /// <summary>
        /// 按钮的名称(name)
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 按钮带的参数(name:id)中，id为参数
        /// </summary>
        public string Argument { get; set; }

        public MultiButtonAttribute()
        {             
        }
        public MultiButtonAttribute( string name)
        {
            this.Name = name;
        }

        public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
        {
            
            var key = ButtonKeyFrom(controllerContext);
            if (key == null)
                return false;
            var keyIsValid = IsValid(key);
            if (keyIsValid)
            {
                UpdateValueProviderIn(controllerContext, ValueFrom(key));
            } 
            return keyIsValid;
        }

        private string ButtonKeyFrom(ControllerContext controllerContext)
        {
            var keys = controllerContext.HttpContext.Request.Params.AllKeys;
            return keys.FirstOrDefault(KeyStartsWithButtonName);
        }
        private bool IsValid(string key)
        {
            bool rs = true;
             
            if (!string.IsNullOrEmpty(Argument))
            {
                if (string.IsNullOrEmpty(key))
                    rs = false;
                else
                {
                    var paras = Argument.Split(',');
                    rs = key.Split(',').Length >= Argument.Split(',').Length;
                }
            }
            return rs;
        }

        private static string ValueFrom(string key)
        {
            if (string.IsNullOrEmpty(key))
                return null;
            var parts = key.Split(":".ToCharArray());
            return parts.Length < 2 ? null : parts[1];
        }

        private void UpdateValueProviderIn(ControllerContext controllerContext, string value)
        {
            if (string.IsNullOrEmpty(Argument))
                return;

            var args = Argument.Split(',');
            var vals = value.Split('&');
            for (int i = 0; i < args.Length && i < vals.Length; i++)
            {
                controllerContext.RouteData.Values[args[i]] = vals[i];
            }
        }

        private bool KeyStartsWithButtonName(string key)
        {
            if (key == null) return false;
            key = key.Split(':')[0];
            return key.Equals(Name, StringComparison.InvariantCultureIgnoreCase);
        }

    }
}